package xmpp.androidpush.aidl;

import android.os.Handler;
import android.os.RemoteException;

import xmpp.androidpush.aidl.IPushListener.Stub;
import xmpp.androidpush.util.ConnectionState;

public class PushListener extends Stub {
    private static Handler mHandler = new Handler();
    private PushListener.PushCallback mCallback;

    public PushListener(PushListener.PushCallback callback) {
        this.mCallback = callback;
    }

    public void receivedMessage(final PushMessage msg) throws RemoteException {
        mHandler.post(new Runnable() {
            public void run() {
                PushListener.this.mCallback.receivedMessage(msg);
            }
        });
    }

    @Override
    public void receivedIqMessage(final String msgJson) throws RemoteException {
        mHandler.post(new Runnable() {
            public void run() {
                PushListener.this.mCallback.receivedIqMessage(msgJson);
            }
        });
    }

    public void connectionChanged(final int state, final String msg) throws RemoteException {
        mHandler.post(new Runnable() {
            public void run() {
                PushListener.this.mCallback.connectionChanged(ConnectionState.values()[state], msg);
            }
        });
    }

    public interface PushCallback {
        void receivedMessage(PushMessage msg);
        void receivedIqMessage(String msg);

        void connectionChanged(ConnectionState state, String msg);
    }
}
